package longestExp;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * Date:2024/12/10
 * @author Isabella
 */
public class LongestExpDriver {

    public static void main(String[] args) throws Exception {

        // 创建一个配置对象
        Configuration conf = new Configuration();

        // 创建 MapReduce 作业
        Job job = Job.getInstance(conf);

        // 设置作业的Jar类
        job.setJarByClass(LongestExpDriver.class);

        // 设置 Mapper 和 Reducer 类
        job.setMapperClass(LongestExpMapper.class);
        job.setReducerClass(LongestExpReducer.class);

        // 设置 map 阶段输出的 Key 和 Value 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 设置 reduce 阶段输出的 Key 和 Value 类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 设置输入和输出路径
        Path intputPath = new Path("input/jobData.csv");
        Path outputPath = new Path("output/LongestExp");

        FileInputFormat.addInputPath(job, intputPath);
        FileOutputFormat.setOutputPath(job, outputPath);

        // 删除多余路径
        FileSystem fs = FileSystem.get(conf);
        if(fs.exists(outputPath)){
            fs.delete(outputPath,true);
        }

        // 提交作业并等待完成
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

}
